package logic

import (
	"context"
	"errors"
	"taobao-merchandise/mysql/models"

	"taobao-merchandise/merchandiserpc/internal/svc"
	"taobao-merchandise/merchandiserpc/merchandiserpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopDelLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopDelLogic {
	return &ShopDelLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品删除
func (l *ShopDelLogic) ShopDel(in *merchandiserpc.ShopDelRequest) (*merchandiserpc.ShopDelResponse, error) {
	// todo: add your logic here and delete this line

	var user models.User
	models.DB.Debug().Where("id = ?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("当前用户不存在,删除失败")
	}

	var shop models.Shop
	models.DB.Debug().Where("id = ?", in.ShopId).Find(&shop)
	if shop.ID == 0 {
		return nil, errors.New("当前商品不存在,删除失败")
	}

	if in.UserId != shop.UserId {
		return nil, errors.New("您无权限删除该商品")
	}

	models.DB.Debug().Delete(&shop)

	return &merchandiserpc.ShopDelResponse{}, nil
}
